package com.management.vtribe.service;

import com.management.vtribe.Bean.Article;
import com.management.vtribe.mapper.ArticleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ArticleService {
    @Autowired
    ArticleMapper articleMapper;

    public List<Article> getAllArticle(int page, int num){
        return articleMapper.getAllArticle(page*num,num);
    }

    public int getAllArticleCount(){
        return articleMapper.getAllArticleCount();
    }

    public  List<Article> getArticleByTitle(String title){
        return articleMapper.getArticleByTitle(title);
    }

    public List<Article> getAllArticleByState(String state,int page,int num){
        return articleMapper.getAllArticleByState(state, page*num, num);
    }

    public int getStateArticleCount(String state){
        return articleMapper.getStateArticleCount(state);
    }

    public List<Article> getArticleByTS(String title,String state){
        return articleMapper.getArticleByTS(title, state);
    }

    public int deleteArticleById(int[] ids){
        int res1 = articleMapper.deleteArticleContentById(ids);
        int res2 = articleMapper.deleteArticleInfoById(ids);
        if(res1==res2){
            return res1;
        }else{
            return 0;
        }
    }

    public int updateStateById(int[] ids,String state){
        return articleMapper.updateStateById(ids, state);
    }

    public int updateTCById(int id,String title,int cid){return articleMapper.updateTCById(id, title, cid);}

    public  int updateArticleById(int id,String content,String title,int cid,String state){
        int[] ids = new int[1];
        ids[0]=id;
        int res3 = articleMapper.updateStateById(ids,state);
        int res1 = articleMapper.updateTCById(id, title, cid);
        int res2 = articleMapper.updateContentById(id, content);
        if(res1==res2&&res1==res3){
            return res1;
        }else{
            return 0;
        }
    };

    public int insertArticle(String title,int author,int category,String state,String content){
        int res1 = articleMapper.insertArticleInfo(title, author, category, state);
        if(res1==1){
            int res2 = articleMapper.insertArticleContent(content);
            if(res2==1){
                return res2;
            }else {
                return 0;
            }
        }else{
            return 0;
        }
    }

    public int pvIncrements(){
        Map<Integer,Integer> pv = new HashMap<>();
        int num = articleMapper.getStateArticleCount("发表");
        List<Article> list = articleMapper.getAllArticleByState("发表", 0, num);
        for (Article a:list) {
            pv.put(a.getId(),(int)(Math.random()*10));
        }
        return articleMapper.pvIncrements(pv);
    }

    public List<Integer> getDataStatistics(){
        return articleMapper.getDataStatistics();
    }

    public void pvStatisticsPerDay(){
        articleMapper.pvStatisticsPerDay();
    }

    public List<String> getCategories(){
        return articleMapper.getCategories();
    }
}
